package com.extremelylargepackage.editor;

import java.io.File;

import com.extremelylargepackage.FileUtils;
import com.extremelylargepackage.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LoadLevel extends ActivityForcePortrait 
{
	private ListView _list;
	
	@Override 
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.load_level);
		_list = (ListView) findViewById(R.id.level_list);
		findViewById(R.id.level_list_return).setOnClickListener(new ReturnHandler());
		File levelsDir = FileUtils.getCustomLevelsDir(this);
		constructList(levelsDir);
	}
	
	private void constructList(File dir)
	{
		String[] levels = dir.list();
		if(levels == null || levels.length < 1)
		{
			levels = new String[1];
			levels[0] = "No levels found.";
		}
		else
		{
			_list.setOnItemClickListener(new LevelSelectHandler());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, levels);
		_list.setAdapter(adapter);
	}
	
	private class LevelSelectHandler implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int row, long id) 
		{
			Intent i = new Intent();
			i.putExtra("name", _list.getItemAtPosition(row).toString());
			setResult(Activity.RESULT_OK, i);
			finish();
		}
	}
	
	private class ReturnHandler implements OnClickListener
	{
		@Override
		public void onClick(View v)
		{
			setResult(Activity.RESULT_CANCELED);
			finish();
		}
	}
}
